# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

# 广度优先+递归判断

# 如果n2是None：True；否则如果n1是None：False；否则判断子结点

class Solution:
    def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
        def is_sub(n1, n2):
            if n2:
                if n1:
                    return n1.val == n2.val and is_sub(n1.left, n2.left) and is_sub(n1.right, n2.right)
                else:
                    return False
            return True
        
        if B and A:
            queue = [A]
            while queue:
                node = queue.pop(0)
                if is_sub(node, B):
                    return True
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return False
        